﻿using MediatR;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;
using static System.Net.Mime.MediaTypeNames;

namespace SheepCultivation.API.AddliCation.BreedingD
{
    public class AppearancesCreateHandler : IRequestHandler<AppearancesCreateCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 品种仓储
        /// </summary>
        public readonly IBaseRepository<Sheepvariety> SheepvarietyRepository;
        /// <summary>
        /// 羊只称重仓储
        /// </summary>
        public readonly IBaseRepository<SheepWeighed> SheepWeighedRepository;
        /// <summary>
        /// 耳号仓储
        /// </summary>
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        /// <summary>
        /// 栋舍仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        /// <summary>
        /// 栋舍类型仓储
        /// </summary> 
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        /// <summary>
        /// 栏位仓储
        /// </summary>
        public readonly IBaseRepository<Columns> ColumnsRepository;
        /// <summary>
        /// 品相测定仓储
        /// </summary>
        public readonly IBaseRepository<Appearances> AppearancesRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<AppearancesCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sheepvarietyRepository">品种</param>
        /// <param name="SheepWeighedRepository">羊只称重</param>
        /// <param name="BasicInformationRepository"></param>
        /// <param name="BuildingRepository">耳号</param>
        /// <param name="BuildingTypeRepository">栋舍</param>
        /// <param name="ColumnsRepository">栋舍类型</param>
        /// <param name="BodyRepository">体尺测定</param>
        /// <param name="logger">日志</param>
        public AppearancesCreateHandler(IBaseRepository<Sheepvariety> sheepvarietyRepository, IBaseRepository<SheepWeighed> SheepWeighedRepository, IBaseRepository<BasicInformation> BasicInformationRepository, IBaseRepository<Building> BuildingRepository, IBaseRepository<BuildingType> BuildingTypeRepository, IBaseRepository<Columns> ColumnsRepository, IBaseRepository<Appearances> AppearancesRepository, ILogger<AppearancesCreateHandler> logger)
        {
            this.SheepvarietyRepository = sheepvarietyRepository;
            this.SheepWeighedRepository = SheepWeighedRepository;
            this.BasicInformationRepository = BasicInformationRepository;
            this.BuildingRepository = BuildingRepository;
            this.BuildingTypeRepository = BuildingTypeRepository;
            this.ColumnsRepository = ColumnsRepository;
            this.AppearancesRepository = AppearancesRepository;
            this.logger = logger;
        }

        #endregion

        #region 品相测定新增
        /// <summary>
        /// 品相测定新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(AppearancesCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {
                logger.LogInformation("品相测定新增成功!");
                var res = new Appearances()
                {
                    BreedingSheepArchiveId = request.BreedingSheepArchiveId,
                    Sexs = request.Sexs,
                    AppearancesDate = request.AppearancesDate,
                    AppearancesAge = request.AppearancesAge,
                    Teeth = request.Teeth,
                    AppearancesHair = request.AppearancesHair,
                    AppearancesPigmentation = request.AppearancesPigmentation,
                    AppearancesHoofColor = request.AppearancesHoofColor,
                    AppearancesMouthColor = request.AppearancesMouthColor,
                    Horns = request.Horns,
                    Distribution = request.Distribution
                };
                AppearancesRepository.Add(res);

                result.Code = (int)CommonEnumCode.success;
                result.Msg = "品相测定新增成功";
            }
            catch (Exception ex)
            {
                logger.LogError("品相测定新增失败!" + ex.Message);
                result.Msg = "品相测定新增失败";
            }
            return result;
        }
        #endregion

    }
}
